package com.suse.generator.mybatis.plus;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;

import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * 建造者模式
 */
public class DataSourceConfigBuilder {

    private final DataSourceConfig dsc;

    private String module;

    private DataSource dataSource;

    public DataSourceConfigBuilder(DataSourceConfig dsc) {
        this.dsc = dsc;
    }

    public DataSourceConfigBuilder url(String url) {
        dsc.setUrl(url);
        return this;
    }

    public DataSourceConfigBuilder driver(String driver) {
        dsc.setDriverName(driver);
        return this;
    }

    public DataSourceConfigBuilder username(String username) {
        dsc.setUsername(username);
        return this;
    }

    public DataSourceConfigBuilder dataSource(DataSource dataSource) {
        try {
            DatabaseMetaData md = dataSource.getConnection().getMetaData();
            dsc.setUrl(md.getURL());
            dsc.setDriverName(md.getDriverName());
            dsc.setUsername(md.getUserName());
        } catch (SQLException s) {
            s.printStackTrace();
        }
        return this;
    }

    public DataSourceConfigBuilder password(String password) {
        dsc.setPassword(password);
        return this;
    }



}
